{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 确认LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from crewai import LLM\n",
    "\n",
    "my_llm = LLM(\n",
    "    api_key = \"NA\",\n",
    "    model = \"ollama/qwen2.5:14b\",\n",
    "    base_url = \"http://192.168.20.43:11434\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件读取成功！\n",
      "global_df.head()：\n",
      "   月份  温度\n",
      "0   1  18\n",
      "1   2  19\n",
      "2   3  20\n",
      "3   4  20\n",
      "4   5  22\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 定义一个全局变量来存储读取的数据\n",
    "global_df = None\n",
    "file_path = \"Data/温度.xlsx\"\n",
    "\n",
    "def read_data_to_df():\n",
    "    \"\"\"\n",
    "    读取 Excel 文件中的数据并存储到全局变量Dataframe中。\n",
    "\n",
    "    参数:\n",
    "        file_path (str): Excel 文件的路径。\n",
    "    \"\"\"\n",
    "    global global_df  # 声明使用全局变量\n",
    "    global file_path\n",
    "    try:\n",
    "        # 使用 pandas 的 read_excel 函数读取 Excel 文件\n",
    "        global_df = pd.read_excel(file_path)\n",
    "        print(\"文件读取成功！\")\n",
    "        return global_df\n",
    "    except FileNotFoundError:\n",
    "        print(f\"错误：文件 {file_path} 未找到，请检查路径是否正确。\")\n",
    "    except Exception as e:\n",
    "        print(f\"读取文件时发生错误：{e}\")\n",
    "\n",
    "# 调用函数读取数据\n",
    "global_df = read_data_to_df()\n",
    "\n",
    "# 检查全局变量是否已赋值\n",
    "if global_df is not None:\n",
    "    print(\"global_df.head()：\")\n",
    "    print(global_df.head())  # 打印前几行数据\n",
    "else:\n",
    "    print(\"数据读取失败，全局变量未赋值。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据后获取pandas可展示的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df.shape: (12, 2)\n",
      "df.head:\n",
      "   月份  温度\n",
      "0   1  18\n",
      "1   2  19\n",
      "2   3  20\n",
      "3   4  20\n",
      "4   5  22\n",
      "df.dtypes:\n",
      "月份    int64\n",
      "温度    int64\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# 动态生成 DataFrame 的信息\n",
    "def generate_df_info(df):\n",
    "    shape_info = f\"df.shape: {df.shape}\"\n",
    "    head_info = f\"df.head:\\n{df.head()}\"\n",
    "    dtypes_info = f\"df.dtypes:\\n{df.dtypes}\"\n",
    "    return shape_info, head_info, dtypes_info\n",
    "\n",
    "# 获取 DataFrame 的信息\n",
    "shape_info, head_info, dtypes_info = generate_df_info(global_df)\n",
    "print(shape_info)\n",
    "print(head_info)\n",
    "print(dtypes_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建一个agent可以使用的工具"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    月份  温度\n",
      "0    1  18\n",
      "1    2  19\n",
      "2    3  20\n",
      "3    4  20\n",
      "4    5  22\n",
      "5    6  23\n",
      "6    7  27\n",
      "7    8  33\n",
      "8    9  33\n",
      "9   10  22\n",
      "10  11  20\n",
      "11  12   3\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from crewai.tools import BaseTool\n",
    "\n",
    "class ExcelLoaderTool(BaseTool):\n",
    "    name: str = \"ExcelLoaderTool\"\n",
    "    description: str = \"A tool to load data from a Excel file, output is a 'DataFrame' object.\"\n",
    "\n",
    "    def _run(self) -> str:\n",
    "        \"\"\"Load data from the predefined Excel file.\"\"\"\n",
    "        try:\n",
    "            # 固定的文件路径\n",
    "            df = pd.read_excel(file_path)\n",
    "            # 将数据转换为字符串格式返回（这里可以根据需要调整返回格式）\n",
    "            return df\n",
    "        except Exception as e:\n",
    "            return f\"Error loading Excel file: {str(e)}\"\n",
    "\n",
    "# 创建工具实例\n",
    "excel_loader_tool = ExcelLoaderTool()\n",
    "print(excel_loader_tool._run())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m\u001b[95m# Agent:\u001b[00m \u001b[1m\u001b[92m专业的python数据科学家,擅长基于给定问题撰写Python程序\u001b[00m\n",
      "\u001b[95m## Task:\u001b[00m \u001b[92m基于用户给出的问题，编写一个python函数，并执行这个函数，打印出结果。\u001b[00m\n",
      "\u001b[91m \n",
      "\n",
      "I encountered an error while trying to use the tool. This was the error: Arguments validation failed: 1 validation error for CodeInterpreterSchema\n",
      "libraries_used\n",
      "  Field required [type=missing, input_value={'code': \"import pandas a...mperature_trend(data))\"}, input_type=dict]\n",
      "    For further information visit https://errors.pydantic.dev/2.10/v/missing.\n",
      " Tool Code Interpreter accepts these inputs: Tool Name: Code Interpreter\n",
      "Tool Arguments: {'code': {'description': 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', 'type': 'str'}, 'libraries_used': {'description': 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4', 'type': 'list[str]'}}\n",
      "Tool Description: Interprets Python3 code strings with a final print statement.\n",
      "\u001b[00m\n",
      "\n",
      "\n",
      "\u001b[1m\u001b[95m# Agent:\u001b[00m \u001b[1m\u001b[92m专业的python数据科学家,擅长基于给定问题撰写Python程序\u001b[00m\n",
      "\u001b[95m## Using tool:\u001b[00m \u001b[92mCode Interpreter\u001b[00m\n",
      "\u001b[95m## Tool Input:\u001b[00m \u001b[92m\n",
      "\"{\\\"code\\\": \\\"import pandas as pd\\\\nimport matplotlib.pyplot as plt\\\\n\\\\ndef analyze_temperature_trend(df):\\\\n    df['\\\\u6708\\\\u4efd'] = pd.to_datetime(df['\\\\u6708\\\\u4efd'], format='%m').dt.month_name()\\\\n    df.set_index('\\\\u6708\\\\u4efd', inplace=True)\\\\n    plt.figure(figsize=(10,6))\\\\n    plt.plot(df.index, df['\\\\u6e29\\\\u5ea6'])\\\\n    plt.title('\\\\u6e29\\\\u5ea6\\\\u8d8b\\\\u52bf')\\\\n    plt.xlabel('\\\\u6708\\\\u4efd')\\\\n    plt.ylabel('\\\\u6e29\\\\u5ea6')\\\\n    plt.grid(True)\\\\n    plt.xticks(rotation=45)\\\\n    plt.tight_layout()\\\\n    print(analyze_temperature_trend(data))\\\"}\"\u001b[00m\n",
      "\u001b[95m## Tool Output:\u001b[00m \u001b[92m\n",
      "\n",
      "I encountered an error while trying to use the tool. This was the error: Arguments validation failed: 1 validation error for CodeInterpreterSchema\n",
      "libraries_used\n",
      "  Field required [type=missing, input_value={'code': \"import pandas a...mperature_trend(data))\"}, input_type=dict]\n",
      "    For further information visit https://errors.pydantic.dev/2.10/v/missing.\n",
      " Tool Code Interpreter accepts these inputs: Tool Name: Code Interpreter\n",
      "Tool Arguments: {'code': {'description': 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', 'type': 'str'}, 'libraries_used': {'description': 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4', 'type': 'list[str]'}}\n",
      "Tool Description: Interprets Python3 code strings with a final print statement..\n",
      "Moving on then. I MUST either use a tool (use one at time) OR give my best final answer not both at the same time. When responding, I must use the following format:\n",
      "\n",
      "```\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Code Interpreter]\n",
      "Action Input: the input to the action, dictionary enclosed in curly braces\n",
      "Observation: the result of the action\n",
      "```\n",
      "This Thought/Action/Action Input/Result can repeat N times. Once I know the final answer, I must return the following format:\n",
      "\n",
      "```\n",
      "Thought: I now can give a great answer\n",
      "Final Answer: Your final answer must be the great and the most complete as possible, it must be outcome described\n",
      "\n",
      "```\u001b[00m\n",
      "\u001b[91m \n",
      "\n",
      "I encountered an error while trying to use the tool. This was the error: Arguments validation failed: 1 validation error for CodeInterpreterSchema\n",
      "libraries_used\n",
      "  Field required [type=missing, input_value={'code': \"import pandas a...emperature_trend(data)\"}, input_type=dict]\n",
      "    For further information visit https://errors.pydantic.dev/2.10/v/missing.\n",
      " Tool Code Interpreter accepts these inputs: Tool Name: Code Interpreter\n",
      "Tool Arguments: {'code': {'description': 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', 'type': 'str'}, 'libraries_used': {'description': 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4', 'type': 'list[str]'}}\n",
      "Tool Description: Interprets Python3 code strings with a final print statement.\n",
      "\u001b[00m\n",
      "\n",
      "\n",
      "\u001b[1m\u001b[95m# Agent:\u001b[00m \u001b[1m\u001b[92m专业的python数据科学家,擅长基于给定问题撰写Python程序\u001b[00m\n",
      "\u001b[95m## Using tool:\u001b[00m \u001b[92mCode Interpreter\u001b[00m\n",
      "\u001b[95m## Tool Input:\u001b[00m \u001b[92m\n",
      "\"{\\\"code\\\": \\\"import pandas as pd\\\\nimport matplotlib.pyplot as plt\\\\ndata = pd.DataFrame({\\\\\\\\'\\\\u6708\\\\u4efd\\\\\\\\': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], \\\\\\\\'\\\\u6e29\\\\u5ea6\\\\\\\\': [18, 19, 20, 20, 22, 23, 27, 33, 33, 22, 20, 3]})\\\\ndef analyze_temperature_trend(df):\\\\n    df[\\\\\\\\'\\\\u6708\\\\u4efd\\\\\\\\'] = pd.to_datetime(df[\\\\\\\\'\\\\u6708\\\\u4efd\\\\\\\\'], format=\\\\\\\\'%m\\\\\\\\').dt.month_name()\\\\n    df.set_index(\\\\\\\\'\\\\u6708\\\\u4efd\\\\\\\\', inplace=True)\\\\n    plt.figure(figsize=(10,6))\\\\n    plt.plot(df.index, df[\\\\\\\\'\\\\u6e29\\\\u5ea6\\\\\\\\'])\\\\n    plt.title(\\\\\\\\'\\\\u6e29\\\\u5ea6\\\\u8d8b\\\\u52bf\\\\\\\\')\\\\n    plt.xlabel(\\\\\\\\'\\\\u6708\\\\u4efd\\\\\\\\')\\\\n    plt.ylabel(\\\\\\\\'\\\\u6e29\\\\u5ea6\\\\\\\\')\\\\n    plt.grid(True)\\\\n    plt.xticks(rotation=45)\\\\n    plt.tight_layout()\\\\n    return plt.show()\\\\\\\\n\\\\nanalyze_temperature_trend(data)\\\"}\"\u001b[00m\n",
      "\u001b[95m## Tool Output:\u001b[00m \u001b[92m\n",
      "\n",
      "I encountered an error while trying to use the tool. This was the error: Arguments validation failed: 1 validation error for CodeInterpreterSchema\n",
      "libraries_used\n",
      "  Field required [type=missing, input_value={'code': \"import pandas a...emperature_trend(data)\"}, input_type=dict]\n",
      "    For further information visit https://errors.pydantic.dev/2.10/v/missing.\n",
      " Tool Code Interpreter accepts these inputs: Tool Name: Code Interpreter\n",
      "Tool Arguments: {'code': {'description': 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', 'type': 'str'}, 'libraries_used': {'description': 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4', 'type': 'list[str]'}}\n",
      "Tool Description: Interprets Python3 code strings with a final print statement..\n",
      "Moving on then. I MUST either use a tool (use one at time) OR give my best final answer not both at the same time. When responding, I must use the following format:\n",
      "\n",
      "```\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Code Interpreter]\n",
      "Action Input: the input to the action, dictionary enclosed in curly braces\n",
      "Observation: the result of the action\n",
      "```\n",
      "This Thought/Action/Action Input/Result can repeat N times. Once I know the final answer, I must return the following format:\n",
      "\n",
      "```\n",
      "Thought: I now can give a great answer\n",
      "Final Answer: Your final answer must be the great and the most complete as possible, it must be outcome described\n",
      "\n",
      "```\u001b[00m\n",
      "\u001b[91m \n",
      "\n",
      "I encountered an error while trying to use the tool. This was the error: Arguments validation failed: 1 validation error for CodeInterpreterSchema\n",
      "libraries_used\n",
      "  Field required [type=missing, input_value={'code': \"import pandas a...'pandas', 'matplotlib']}, input_type=dict]\n",
      "    For further information visit https://errors.pydantic.dev/2.10/v/missing.\n",
      " Tool Code Interpreter accepts these inputs: Tool Name: Code Interpreter\n",
      "Tool Arguments: {'code': {'description': 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', 'type': 'str'}, 'libraries_used': {'description': 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4', 'type': 'list[str]'}}\n",
      "Tool Description: Interprets Python3 code strings with a final print statement.\n",
      "\u001b[00m\n",
      "\n",
      "\n",
      "\u001b[1m\u001b[95m# Agent:\u001b[00m \u001b[1m\u001b[92m专业的python数据科学家,擅长基于给定问题撰写Python程序\u001b[00m\n",
      "\u001b[95m## Using tool:\u001b[00m \u001b[92mCode Interpreter\u001b[00m\n",
      "\u001b[95m## Tool Input:\u001b[00m \u001b[92m\n",
      "\"{\\\"code\\\": \\\"import pandas as pd\\\\\\\\nimport matplotlib.pyplot as plt\\\\\\\\ndef analyze_temperature_trend(df):\\\\\\\\n    df['\\\\u6708\\\\u4efd'] = pd.to_datetime(df['\\\\u6708\\\\u4efd'], format='%m').dt.month_name()\\\\\\\\n    df.set_index('\\\\u6708\\\\u4efd', inplace=True)\\\\\\\\n    plt.figure(figsize=(10,6))\\\\\\\\n    plt.plot(df.index, df['\\\\u6e29\\\\u5ea6'])\\\\\\\\n    plt.title('\\\\u6e29\\\\u5ea6\\\\u8d8b\\\\u52bf')\\\\\\\\n    plt.xlabel('\\\\u6708\\\\u4efd')\\\\\\\\n    plt.ylabel('\\\\u6e29\\\\u5ea6')\\\\\\\\n    plt.grid(True)\\\\\\\\n    plt.xticks(rotation=45)\\\\\\\\n    plt.tight_layout()\\\\\\\\n    return plt.show()\\\\\\\\ndata = pd.DataFrame({'\\\\u6708\\\\u4efd': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], '\\\\u6e29\\\\u5ea6': [18, 19, 20, 20, 22, 23, 27, 33, 33, 22, 20, 3]})\\\\\\\\nanalyze_temperature_trend(data)\\\", \\\"libraries_to_install\\\": [\\\"pandas\\\", \\\"matplotlib\\\"]}\"\u001b[00m\n",
      "\u001b[95m## Tool Output:\u001b[00m \u001b[92m\n",
      "\n",
      "I encountered an error while trying to use the tool. This was the error: Arguments validation failed: 1 validation error for CodeInterpreterSchema\n",
      "libraries_used\n",
      "  Field required [type=missing, input_value={'code': \"import pandas a...'pandas', 'matplotlib']}, input_type=dict]\n",
      "    For further information visit https://errors.pydantic.dev/2.10/v/missing.\n",
      " Tool Code Interpreter accepts these inputs: Tool Name: Code Interpreter\n",
      "Tool Arguments: {'code': {'description': 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', 'type': 'str'}, 'libraries_used': {'description': 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4', 'type': 'list[str]'}}\n",
      "Tool Description: Interprets Python3 code strings with a final print statement..\n",
      "Moving on then. I MUST either use a tool (use one at time) OR give my best final answer not both at the same time. When responding, I must use the following format:\n",
      "\n",
      "```\n",
      "Thought: you should always think about what to do\n",
      "Action: the action to take, should be one of [Code Interpreter]\n",
      "Action Input: the input to the action, dictionary enclosed in curly braces\n",
      "Observation: the result of the action\n",
      "```\n",
      "This Thought/Action/Action Input/Result can repeat N times. Once I know the final answer, I must return the following format:\n",
      "\n",
      "```\n",
      "Thought: I now can give a great answer\n",
      "Final Answer: Your final answer must be the great and the most complete as possible, it must be outcome described\n",
      "\n",
      "```\u001b[00m\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 51\u001b[0m\n\u001b[0;32m     43\u001b[0m analysis_crew \u001b[38;5;241m=\u001b[39m Crew(\n\u001b[0;32m     44\u001b[0m     agents\u001b[38;5;241m=\u001b[39m[coding_agent],\n\u001b[0;32m     45\u001b[0m     tasks\u001b[38;5;241m=\u001b[39m[coding_task]\n\u001b[0;32m     46\u001b[0m )\n\u001b[0;32m     49\u001b[0m \u001b[38;5;66;03m# 测试代码\u001b[39;00m\n\u001b[1;32m---> 51\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43manalysis_crew\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mkickoff\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     52\u001b[0m \u001b[38;5;28mprint\u001b[39m(result)\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\crew.py:558\u001b[0m, in \u001b[0;36mCrew.kickoff\u001b[1;34m(self, inputs)\u001b[0m\n\u001b[0;32m    555\u001b[0m metrics: List[UsageMetrics] \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m    557\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprocess \u001b[38;5;241m==\u001b[39m Process\u001b[38;5;241m.\u001b[39msequential:\n\u001b[1;32m--> 558\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_run_sequential_process\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    559\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprocess \u001b[38;5;241m==\u001b[39m Process\u001b[38;5;241m.\u001b[39mhierarchical:\n\u001b[0;32m    560\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_run_hierarchical_process()\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\crew.py:665\u001b[0m, in \u001b[0;36mCrew._run_sequential_process\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    663\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_run_sequential_process\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m CrewOutput:\n\u001b[0;32m    664\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Executes tasks sequentially and returns the final output.\"\"\"\u001b[39;00m\n\u001b[1;32m--> 665\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_tasks\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtasks\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\crew.py:767\u001b[0m, in \u001b[0;36mCrew._execute_tasks\u001b[1;34m(self, tasks, start_index, was_replayed)\u001b[0m\n\u001b[0;32m    764\u001b[0m     futures\u001b[38;5;241m.\u001b[39mclear()\n\u001b[0;32m    766\u001b[0m context \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_context(task, task_outputs)\n\u001b[1;32m--> 767\u001b[0m task_output \u001b[38;5;241m=\u001b[39m \u001b[43mtask\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute_sync\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    768\u001b[0m \u001b[43m    \u001b[49m\u001b[43magent\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43magent_to_use\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    769\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcontext\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcontext\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    770\u001b[0m \u001b[43m    \u001b[49m\u001b[43mtools\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtools_for_task\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    771\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    772\u001b[0m task_outputs \u001b[38;5;241m=\u001b[39m [task_output]\n\u001b[0;32m    773\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_process_task_result(task, task_output)\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\task.py:302\u001b[0m, in \u001b[0;36mTask.execute_sync\u001b[1;34m(self, agent, context, tools)\u001b[0m\n\u001b[0;32m    295\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mexecute_sync\u001b[39m(\n\u001b[0;32m    296\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m    297\u001b[0m     agent: Optional[BaseAgent] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m    298\u001b[0m     context: Optional[\u001b[38;5;28mstr\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m    299\u001b[0m     tools: Optional[List[BaseTool]] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m    300\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m TaskOutput:\n\u001b[0;32m    301\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Execute the task synchronously.\"\"\"\u001b[39;00m\n\u001b[1;32m--> 302\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_core\u001b[49m\u001b[43m(\u001b[49m\u001b[43magent\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtools\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\task.py:366\u001b[0m, in \u001b[0;36mTask._execute_core\u001b[1;34m(self, agent, context, tools)\u001b[0m\n\u001b[0;32m    362\u001b[0m tools \u001b[38;5;241m=\u001b[39m tools \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtools \u001b[38;5;129;01mor\u001b[39;00m []\n\u001b[0;32m    364\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprocessed_by_agents\u001b[38;5;241m.\u001b[39madd(agent\u001b[38;5;241m.\u001b[39mrole)\n\u001b[1;32m--> 366\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43magent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute_task\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    367\u001b[0m \u001b[43m    \u001b[49m\u001b[43mtask\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m    368\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcontext\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcontext\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    369\u001b[0m \u001b[43m    \u001b[49m\u001b[43mtools\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtools\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    370\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    372\u001b[0m pydantic_output, json_output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_export_output(result)\n\u001b[0;32m    373\u001b[0m task_output \u001b[38;5;241m=\u001b[39m TaskOutput(\n\u001b[0;32m    374\u001b[0m     name\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname,\n\u001b[0;32m    375\u001b[0m     description\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdescription,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    381\u001b[0m     output_format\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_output_format(),\n\u001b[0;32m    382\u001b[0m )\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\agent.py:248\u001b[0m, in \u001b[0;36mAgent.execute_task\u001b[1;34m(self, task, context, tools)\u001b[0m\n\u001b[0;32m    245\u001b[0m     task_prompt \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_use_trained_data(task_prompt\u001b[38;5;241m=\u001b[39mtask_prompt)\n\u001b[0;32m    247\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 248\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43magent_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    249\u001b[0m \u001b[43m        \u001b[49m\u001b[43m{\u001b[49m\n\u001b[0;32m    250\u001b[0m \u001b[43m            \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43minput\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtask_prompt\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    251\u001b[0m \u001b[43m            \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtool_names\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43magent_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtools_names\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    252\u001b[0m \u001b[43m            \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtools\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43magent_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtools_description\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    253\u001b[0m \u001b[43m            \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mask_for_human_input\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtask\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhuman_input\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    254\u001b[0m \u001b[43m        \u001b[49m\u001b[43m}\u001b[49m\n\u001b[0;32m    255\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124moutput\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[0;32m    256\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m    257\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m e\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__module__\u001b[39m\u001b[38;5;241m.\u001b[39mstartswith(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlitellm\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[0;32m    258\u001b[0m         \u001b[38;5;66;03m# Do not retry on litellm errors\u001b[39;00m\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\agents\\crew_agent_executor.py:102\u001b[0m, in \u001b[0;36mCrewAgentExecutor.invoke\u001b[1;34m(self, inputs)\u001b[0m\n\u001b[0;32m     99\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mask_for_human_input \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mbool\u001b[39m(inputs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mask_for_human_input\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m))\n\u001b[0;32m    101\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 102\u001b[0m     formatted_answer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_invoke_loop\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    103\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mAssertionError\u001b[39;00m:\n\u001b[0;32m    104\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_printer\u001b[38;5;241m.\u001b[39mprint(\n\u001b[0;32m    105\u001b[0m         content\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAgent failed to reach a final answer. This is likely a bug - please report it.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m    106\u001b[0m         color\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mred\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m    107\u001b[0m     )\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\agents\\crew_agent_executor.py:144\u001b[0m, in \u001b[0;36mCrewAgentExecutor._invoke_loop\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    141\u001b[0m formatted_answer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_process_llm_response(answer)\n\u001b[0;32m    143\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(formatted_answer, AgentAction):\n\u001b[1;32m--> 144\u001b[0m     tool_result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_tool_and_check_finality\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    145\u001b[0m \u001b[43m        \u001b[49m\u001b[43mformatted_answer\u001b[49m\n\u001b[0;32m    146\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    147\u001b[0m     formatted_answer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_handle_agent_action(\n\u001b[0;32m    148\u001b[0m         formatted_answer, tool_result\n\u001b[0;32m    149\u001b[0m     )\n\u001b[0;32m    151\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_invoke_step_callback(formatted_answer)\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\agents\\crew_agent_executor.py:374\u001b[0m, in \u001b[0;36mCrewAgentExecutor._execute_tool_and_check_finality\u001b[1;34m(self, agent_action)\u001b[0m\n\u001b[0;32m    368\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    369\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m tool_calling\u001b[38;5;241m.\u001b[39mtool_name\u001b[38;5;241m.\u001b[39mcasefold()\u001b[38;5;241m.\u001b[39mstrip() \u001b[38;5;129;01min\u001b[39;00m [\n\u001b[0;32m    370\u001b[0m         name\u001b[38;5;241m.\u001b[39mcasefold()\u001b[38;5;241m.\u001b[39mstrip() \u001b[38;5;28;01mfor\u001b[39;00m name \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtool_name_to_tool_map\n\u001b[0;32m    371\u001b[0m     ] \u001b[38;5;129;01mor\u001b[39;00m tool_calling\u001b[38;5;241m.\u001b[39mtool_name\u001b[38;5;241m.\u001b[39mcasefold()\u001b[38;5;241m.\u001b[39mreplace(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01min\u001b[39;00m [\n\u001b[0;32m    372\u001b[0m         name\u001b[38;5;241m.\u001b[39mcasefold()\u001b[38;5;241m.\u001b[39mstrip() \u001b[38;5;28;01mfor\u001b[39;00m name \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtool_name_to_tool_map\n\u001b[0;32m    373\u001b[0m     ]:\n\u001b[1;32m--> 374\u001b[0m         tool_result \u001b[38;5;241m=\u001b[39m \u001b[43mtool_usage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43muse\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtool_calling\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43magent_action\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtext\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    375\u001b[0m         tool \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtool_name_to_tool_map\u001b[38;5;241m.\u001b[39mget(tool_calling\u001b[38;5;241m.\u001b[39mtool_name)\n\u001b[0;32m    376\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m tool:\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\tools\\tool_usage.py:131\u001b[0m, in \u001b[0;36mToolUsage.use\u001b[1;34m(self, calling, tool_string)\u001b[0m\n\u001b[0;32m    128\u001b[0m             \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_printer\u001b[38;5;241m.\u001b[39mprint(content\u001b[38;5;241m=\u001b[39m\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00merror\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, color\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mred\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    129\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m error\n\u001b[1;32m--> 131\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_use\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtool_string\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtool_string\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;250;43m \u001b[39;49m\u001b[43mtool\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtool\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;250;43m \u001b[39;49m\u001b[43mcalling\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcalling\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\tools\\tool_usage.py:190\u001b[0m, in \u001b[0;36mToolUsage._use\u001b[1;34m(self, tool_string, tool, calling)\u001b[0m\n\u001b[0;32m    184\u001b[0m     acceptable_args \u001b[38;5;241m=\u001b[39m tool\u001b[38;5;241m.\u001b[39margs_schema\u001b[38;5;241m.\u001b[39mmodel_json_schema()[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mproperties\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39mkeys()  \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[0;32m    185\u001b[0m     arguments \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m    186\u001b[0m         k: v\n\u001b[0;32m    187\u001b[0m         \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m calling\u001b[38;5;241m.\u001b[39marguments\u001b[38;5;241m.\u001b[39mitems()\n\u001b[0;32m    188\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m k \u001b[38;5;129;01min\u001b[39;00m acceptable_args\n\u001b[0;32m    189\u001b[0m     }\n\u001b[1;32m--> 190\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[43mtool\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43marguments\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    191\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m    192\u001b[0m     arguments \u001b[38;5;241m=\u001b[39m calling\u001b[38;5;241m.\u001b[39marguments\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai\\tools\\structured_tool.py:236\u001b[0m, in \u001b[0;36mCrewStructuredTool.invoke\u001b[1;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[0;32m    234\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Main method for tool execution.\"\"\"\u001b[39;00m\n\u001b[0;32m    235\u001b[0m parsed_args \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_parse_args(\u001b[38;5;28minput\u001b[39m)\n\u001b[1;32m--> 236\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mparsed_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai_tools\\tools\\code_interpreter_tool\\code_interpreter_tool.py:83\u001b[0m, in \u001b[0;36mCodeInterpreterTool._run\u001b[1;34m(self, **kwargs)\u001b[0m\n\u001b[0;32m     81\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mrun_code_unsafe(code, libraries_used)\n\u001b[0;32m     82\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m---> 83\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_code_in_docker\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcode\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlibraries_used\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai_tools\\tools\\code_interpreter_tool\\code_interpreter_tool.py:117\u001b[0m, in \u001b[0;36mCodeInterpreterTool.run_code_in_docker\u001b[1;34m(self, code, libraries_used)\u001b[0m\n\u001b[0;32m    115\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_verify_docker_image()\n\u001b[0;32m    116\u001b[0m container \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_init_docker_container()\n\u001b[1;32m--> 117\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_install_libraries\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcontainer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlibraries_used\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    119\u001b[0m exec_result \u001b[38;5;241m=\u001b[39m container\u001b[38;5;241m.\u001b[39mexec_run([\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpython3\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m-c\u001b[39m\u001b[38;5;124m\"\u001b[39m, code])\n\u001b[0;32m    121\u001b[0m container\u001b[38;5;241m.\u001b[39mstop()\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\crewai_tools\\tools\\code_interpreter_tool\\code_interpreter_tool.py:90\u001b[0m, in \u001b[0;36mCodeInterpreterTool._install_libraries\u001b[1;34m(self, container, libraries)\u001b[0m\n\u001b[0;32m     86\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m     87\u001b[0m \u001b[38;5;124;03mInstall missing libraries in the Docker container\u001b[39;00m\n\u001b[0;32m     88\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m     89\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m library \u001b[38;5;129;01min\u001b[39;00m libraries:\n\u001b[1;32m---> 90\u001b[0m     \u001b[43mcontainer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexec_run\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mpip\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43minstall\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlibrary\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\models\\containers.py:213\u001b[0m, in \u001b[0;36mContainer.exec_run\u001b[1;34m(self, cmd, stdout, stderr, stdin, tty, privileged, user, detach, stream, socket, environment, workdir, demux)\u001b[0m\n\u001b[0;32m    170\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    171\u001b[0m \u001b[38;5;124;03mRun a command inside this container. Similar to\u001b[39;00m\n\u001b[0;32m    172\u001b[0m \u001b[38;5;124;03m``docker exec``.\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    206\u001b[0m \u001b[38;5;124;03m        If the server returns an error.\u001b[39;00m\n\u001b[0;32m    207\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    208\u001b[0m resp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclient\u001b[38;5;241m.\u001b[39mapi\u001b[38;5;241m.\u001b[39mexec_create(\n\u001b[0;32m    209\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mid, cmd, stdout\u001b[38;5;241m=\u001b[39mstdout, stderr\u001b[38;5;241m=\u001b[39mstderr, stdin\u001b[38;5;241m=\u001b[39mstdin, tty\u001b[38;5;241m=\u001b[39mtty,\n\u001b[0;32m    210\u001b[0m     privileged\u001b[38;5;241m=\u001b[39mprivileged, user\u001b[38;5;241m=\u001b[39muser, environment\u001b[38;5;241m=\u001b[39menvironment,\n\u001b[0;32m    211\u001b[0m     workdir\u001b[38;5;241m=\u001b[39mworkdir,\n\u001b[0;32m    212\u001b[0m )\n\u001b[1;32m--> 213\u001b[0m exec_output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mclient\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapi\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexec_start\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    214\u001b[0m \u001b[43m    \u001b[49m\u001b[43mresp\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mId\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdetach\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdetach\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtty\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtty\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msocket\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msocket\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    215\u001b[0m \u001b[43m    \u001b[49m\u001b[43mdemux\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdemux\u001b[49m\n\u001b[0;32m    216\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    217\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m socket \u001b[38;5;129;01mor\u001b[39;00m stream:\n\u001b[0;32m    218\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m ExecResult(\u001b[38;5;28;01mNone\u001b[39;00m, exec_output)\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\utils\\decorators.py:19\u001b[0m, in \u001b[0;36mcheck_resource.<locals>.decorator.<locals>.wrapped\u001b[1;34m(self, resource_id, *args, **kwargs)\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m resource_id:\n\u001b[0;32m     16\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m errors\u001b[38;5;241m.\u001b[39mNullResource(\n\u001b[0;32m     17\u001b[0m         \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mResource ID was not provided\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m     18\u001b[0m     )\n\u001b[1;32m---> 19\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mresource_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\api\\exec_api.py:172\u001b[0m, in \u001b[0;36mExecApiMixin.exec_start\u001b[1;34m(self, exec_id, detach, tty, stream, socket, demux)\u001b[0m\n\u001b[0;32m    169\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m socket:\n\u001b[0;32m    170\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_raw_response_socket(res)\n\u001b[1;32m--> 172\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_read_from_socket\u001b[49m\u001b[43m(\u001b[49m\u001b[43mres\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtty\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtty\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdemux\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdemux\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    173\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m stream:\n\u001b[0;32m    174\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m CancellableStream(output, res)\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\api\\client.py:445\u001b[0m, in \u001b[0;36mAPIClient._read_from_socket\u001b[1;34m(self, response, stream, tty, demux)\u001b[0m\n\u001b[0;32m    442\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    443\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m    444\u001b[0m         \u001b[38;5;66;03m# Wait for all frames, concatenate them, and return the result\u001b[39;00m\n\u001b[1;32m--> 445\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mconsume_socket_output\u001b[49m\u001b[43m(\u001b[49m\u001b[43mgen\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdemux\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdemux\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    446\u001b[0m     \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m    447\u001b[0m         response\u001b[38;5;241m.\u001b[39mclose()\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\utils\\socket.py:155\u001b[0m, in \u001b[0;36mconsume_socket_output\u001b[1;34m(frames, demux)\u001b[0m\n\u001b[0;32m    141\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    142\u001b[0m \u001b[38;5;124;03mIterate through frames read from the socket and return the result.\u001b[39;00m\n\u001b[0;32m    143\u001b[0m \n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    150\u001b[0m \u001b[38;5;124;03m        concatenation of frames belonging to the same stream.\u001b[39;00m\n\u001b[0;32m    151\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    152\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m demux \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m:\n\u001b[0;32m    153\u001b[0m     \u001b[38;5;66;03m# If the streams are multiplexed, the generator returns strings, that\u001b[39;00m\n\u001b[0;32m    154\u001b[0m     \u001b[38;5;66;03m# we just need to concatenate.\u001b[39;00m\n\u001b[1;32m--> 155\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124;43mb\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mframes\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    157\u001b[0m \u001b[38;5;66;03m# If the streams are demultiplexed, the generator yields tuples\u001b[39;00m\n\u001b[0;32m    158\u001b[0m \u001b[38;5;66;03m# (stdout, stderr)\u001b[39;00m\n\u001b[0;32m    159\u001b[0m out \u001b[38;5;241m=\u001b[39m [\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;28;01mNone\u001b[39;00m]\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\api\\client.py:438\u001b[0m, in \u001b[0;36m<genexpr>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m    435\u001b[0m     gen \u001b[38;5;241m=\u001b[39m (demux_adaptor(\u001b[38;5;241m*\u001b[39mframe) \u001b[38;5;28;01mfor\u001b[39;00m frame \u001b[38;5;129;01min\u001b[39;00m gen)\n\u001b[0;32m    436\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    437\u001b[0m     \u001b[38;5;66;03m# The generator will output strings\u001b[39;00m\n\u001b[1;32m--> 438\u001b[0m     gen \u001b[38;5;241m=\u001b[39m \u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43m_\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mgen\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    440\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m stream:\n\u001b[0;32m    441\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m gen\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\utils\\socket.py:112\u001b[0m, in \u001b[0;36mframes_iter_no_tty\u001b[1;34m(socket)\u001b[0m\n\u001b[0;32m    107\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    108\u001b[0m \u001b[38;5;124;03mReturns a generator of data read from the socket when the tty setting is\u001b[39;00m\n\u001b[0;32m    109\u001b[0m \u001b[38;5;124;03mnot enabled.\u001b[39;00m\n\u001b[0;32m    110\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    111\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m--> 112\u001b[0m     (stream, n) \u001b[38;5;241m=\u001b[39m \u001b[43mnext_frame_header\u001b[49m\u001b[43m(\u001b[49m\u001b[43msocket\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    113\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m n \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m    114\u001b[0m         \u001b[38;5;28;01mbreak\u001b[39;00m\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\utils\\socket.py:84\u001b[0m, in \u001b[0;36mnext_frame_header\u001b[1;34m(socket)\u001b[0m\n\u001b[0;32m     77\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m     78\u001b[0m \u001b[38;5;124;03mReturns the stream and size of the next frame of data waiting to be read\u001b[39;00m\n\u001b[0;32m     79\u001b[0m \u001b[38;5;124;03mfrom socket, according to the protocol defined here:\u001b[39;00m\n\u001b[0;32m     80\u001b[0m \n\u001b[0;32m     81\u001b[0m \u001b[38;5;124;03mhttps://docs.docker.com/engine/api/v1.24/#attach-to-a-container\u001b[39;00m\n\u001b[0;32m     82\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m     83\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m---> 84\u001b[0m     data \u001b[38;5;241m=\u001b[39m \u001b[43mread_exactly\u001b[49m\u001b[43m(\u001b[49m\u001b[43msocket\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m8\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     85\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m SocketError:\n\u001b[0;32m     86\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m (\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\utils\\socket.py:69\u001b[0m, in \u001b[0;36mread_exactly\u001b[1;34m(socket, n)\u001b[0m\n\u001b[0;32m     67\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     68\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(data) \u001b[38;5;241m<\u001b[39m n:\n\u001b[1;32m---> 69\u001b[0m     next_data \u001b[38;5;241m=\u001b[39m \u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[43msocket\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mn\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     70\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m next_data:\n\u001b[0;32m     71\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m SocketError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnexpected EOF\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\utils\\socket.py:44\u001b[0m, in \u001b[0;36mread\u001b[1;34m(socket, n)\u001b[0m\n\u001b[0;32m     42\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m     43\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(socket, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrecv\u001b[39m\u001b[38;5;124m'\u001b[39m):\n\u001b[1;32m---> 44\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43msocket\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecv\u001b[49m\u001b[43m(\u001b[49m\u001b[43mn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     45\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(socket, pysocket\u001b[38;5;241m.\u001b[39mSocketIO):\n\u001b[0;32m     46\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mread(n)\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\transport\\npipesocket.py:25\u001b[0m, in \u001b[0;36mcheck_closed.<locals>.wrapped\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m     21\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_closed:\n\u001b[0;32m     22\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[0;32m     23\u001b[0m         \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mCan not reuse socket after connection was closed.\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m     24\u001b[0m     )\n\u001b[1;32m---> 25\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Python312\\Lib\\site-packages\\docker\\transport\\npipesocket.py:121\u001b[0m, in \u001b[0;36mNpipeSocket.recv\u001b[1;34m(self, bufsize, flags)\u001b[0m\n\u001b[0;32m    119\u001b[0m \u001b[38;5;129m@check_closed\u001b[39m\n\u001b[0;32m    120\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrecv\u001b[39m(\u001b[38;5;28mself\u001b[39m, bufsize, flags\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m):\n\u001b[1;32m--> 121\u001b[0m     err, data \u001b[38;5;241m=\u001b[39m \u001b[43mwin32file\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mReadFile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_handle\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbufsize\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    122\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m data\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "from crewai import Agent, Task, Crew\n",
    "\n",
    "from crewai_tools import FileReadTool\n",
    "\n",
    "#file_read_tool = FileReadTool(file_path=file_path, encoding='gkb')\n",
    "\n",
    "background = f\"\"\"\n",
    "需要处理的数据形式为pandas的DataFrame.以下是dataframe的一些信息，阅读以下数据了解表格的结构和含义：\n",
    "{global_df.info}\n",
    "{global_df.head()}\n",
    "{global_df.dtypes}\n",
    "\"\"\"\n",
    "\n",
    "goal = \"帮我分析下这个温度数据，看看是否存在什么趋势\"\n",
    "\n",
    "coding_agent = Agent(\n",
    "    role=\"专业的python数据科学家,擅长基于给定问题撰写Python程序\",\n",
    "    goal=f\"为给定的数据分析任务生成一个python函数.任务是：{goal}\",\n",
    "    backstory=background,\n",
    "    verbose=True,\n",
    "    allow_delegation=False,\n",
    "    llm = my_llm,\n",
    "    # tools=[excel_loader_tool],  # 将事件存储传递给 Agent, \n",
    "    allow_code_execution=True\n",
    ")\n",
    "\n",
    "analysis_agent = Agent(\n",
    "    role=\"专业的数据科学家,擅长各种数据分析任务\",\n",
    "    goal=\"根据给定的数据集进行深度分析\",\n",
    "    backstory=background,\n",
    "    verbose=True,\n",
    "    allow_delegation=False,\n",
    "    llm = my_llm,\n",
    "    # tools=[excel_loader_tool],  # 将事件存储传递给 Agent, \n",
    ")\n",
    "\n",
    "coding_task = Task(\n",
    "    description=\"基于用户给出的问题，编写一个python函数，并执行这个函数，打印出结果。\",\n",
    "    agent=coding_agent,\n",
    "    #expected_output=\"请直接给出你的代码，代码中不需要import,不需要注释, 不需要调用的示例代码，不要添加任何代码段标记，只需要给出python函数的定义代码，并执行这个函数，打印出结果。\"\n",
    "    expected_output=\"Return the optimized Python code, execute the code, and display the output.\"\n",
    ")\n",
    "\n",
    "analysis_crew = Crew(\n",
    "    agents=[coding_agent],\n",
    "    tasks=[coding_task]\n",
    ")\n",
    "\n",
    "\n",
    "# 测试代码\n",
    "\n",
    "result = analysis_crew.kickoff()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 检测docker是否正确安装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil\n",
    "import subprocess\n",
    "def validate_docker_installation() -> None:\n",
    "    \"\"\"Check if Docker is installed and running.\"\"\"\n",
    "    if not shutil.which(\"docker\"):\n",
    "        raise RuntimeError(\n",
    "            f\"Docker is not installed. Please install Docker to use code execution with agent: \"\n",
    "        )\n",
    "    else:\n",
    "        print(\"Docker is installed and running.\")\n",
    "\n",
    "    try:\n",
    "        subprocess.run(\n",
    "            [\"docker\", \"info\"],\n",
    "            check=True,\n",
    "            stdout=subprocess.PIPE,\n",
    "            stderr=subprocess.PIPE,\n",
    "        )\n",
    "    except subprocess.CalledProcessError:\n",
    "        raise RuntimeError(\n",
    "            f\"Docker is not running. Please start Docker to use code execution with agent: \"\n",
    "        )\n",
    "validate_docker_installation()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
